package com.fastjrun;

import com.aliyun.alidns20150109.Client;
import com.aliyun.alidns20150109.models.*;
import com.aliyun.darabonba.env.EnvClient;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Sample {

    private static final Logger log = LoggerFactory.getLogger(Sample.class);

    /**
     * Initialization  初始化公共请求参数
     */
    public static Client Initialization(String regionId) throws Exception {
        Config config = new Config();
        // 您的AccessKey ID
        config.accessKeyId = EnvClient.getEnv("ACCESS_KEY_ID");
        // 您的AccessKey Secret
        config.accessKeySecret = EnvClient.getEnv("ACCESS_KEY_SECRET");
        // 您的可用区ID
        config.regionId = regionId;
        return new Client(config);
    }

    /**
     * 获取主域名的所有解析记录列表
     */
    public static DescribeDomainRecordsResponse DescribeDomainRecords(Client client, String domainName, String RR, String recordType) throws Exception {
        DescribeDomainRecordsRequest req = new DescribeDomainRecordsRequest();
        // 主域名
        req.domainName = domainName;
        // 主机记录
        req.RRKeyWord = RR;
        // 解析记录类型
        req.type = recordType;
        try {
            DescribeDomainRecordsResponse resp = client.describeDomainRecords(req);
            log.info("-------------------获取主域名的所有解析记录列表--------------------");
            log.info(Common.toJSONString(TeaModel.buildMap(resp)));
            return resp;
        } catch (TeaException error) {
            log.info(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            log.info(error.message);
        }
        return null;
    }

    /**
     * 修改解析记录
     */
    public static void UpdateDomainRecord(com.aliyun.alidns20150109.Client client, UpdateDomainRecordRequest req) throws Exception {
        try {
            UpdateDomainRecordResponse resp = client.updateDomainRecord(req);
            log.info("-------------------修改解析记录--------------------");
            log.info(Common.toJSONString(TeaModel.buildMap(resp)));
        } catch (TeaException error) {
            log.info(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            log.info(error.message);
        }
    }

    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        String regionid = args.get(0);
        String currentHostIP = args.get(1);
        String domainName = args.get(2);
        String RR = args.get(3);
        String recordType = args.get(4);
        Client client = Sample.Initialization(regionid);
        DescribeDomainRecordsResponse resp = Sample.DescribeDomainRecords(client, domainName, RR, recordType);
        if (Common.isUnset(TeaModel.buildMap(resp)) || Common.isUnset(TeaModel.buildMap(resp.body.domainRecords.record.get(0)))) {
            log.info("错误参数！");
            return;
        }

        DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord record = resp.body.domainRecords.record.get(0);
        // 记录ID
        String recordId = record.recordId;
        // 记录值
        String recordsValue = record.value;
        log.info("-------------------当前主机公网IP为：" + currentHostIP + "--------------------");
        if (!Common.equalString(currentHostIP, recordsValue)) {
            // 修改解析记录
            UpdateDomainRecordRequest req = new UpdateDomainRecordRequest();
            // 主机记录
            req.RR = RR;
            // 记录ID
            req.recordId = recordId;
            // 将主机记录值改为当前主机IP
            req.value = currentHostIP;
            // 解析记录类型
            req.type = recordType;
            Sample.UpdateDomainRecord(client, req);
        }

    }
}
